package com.yingsheng.bi.hks.basic.utils;

import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.util.Collection;
import java.util.Map;
import java.util.UUID;

/**
 * 工具类
 *
 * @author root
 */
public class Utils {

    private static final int MIN_CAPTCHA_VALUE = 100000; // 最小六位数

    private static final int MAX_CAPTCHA_VALUE = 999999; // 最大六位数

    // 静态初始化 SecureRandom 实例，以提高性能和安全性
    private static final SecureRandom secureRandom = new SecureRandom();

    /**
     * 生成六位数的短信验证。
     * 使用 SecureRandom 生成一个介于 [MIN_CAPTCHA_VALUE, MAX_CAPTCHA_VALUE] 的随机整数
     *
     * @return 包含六位随机数字的字符串。
     */
    public static String genSmsCode() {
        int captchaNumber = secureRandom.nextInt(MAX_CAPTCHA_VALUE - MIN_CAPTCHA_VALUE + 1) + MIN_CAPTCHA_VALUE;
        return String.valueOf(captchaNumber);
    }

    /**
     * 生成指定字节数的随机盐（salt）
     * 通常被用来添加到密码或其他输入数据中以增加哈希输出的复杂性和唯一性
     *
     * @param length 随机盐的长度
     * @return
     */
    public static byte[] genSalt(int length) {
        byte[] bytes = new byte[length];
        secureRandom.nextBytes(bytes);
        return bytes;
    }

    public static String genUUid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static boolean isEmpty(String str) {
        return StringUtils.isEmpty(str);
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean isBlank(String str) {
        return !StringUtils.hasText(str);
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    public static boolean isEmpty(Object obj) {
        return ObjectUtils.isEmpty(obj);
    }

    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    public static boolean isEmpty(Collection<?> collection) {
        return CollectionUtils.isEmpty(collection);
    }

    public static boolean isNotEmpty(Collection<?> collection) {
        return !isEmpty(collection);
    }

    public static boolean isEmpty(Map<?, ?> map) {
        return CollectionUtils.isEmpty(map);
    }

    public static boolean isNotEmpty(Map<?, ?> map) {
        return !isEmpty(map);
    }

    public static boolean isAnyEmpty(Object... objects) {
        if (isEmpty(objects)) {
            return true;
        }
        for (Object obj : objects) {
            if (isEmpty(obj)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isAnyEmpty(String... strs) {
        if (isEmpty(strs)) {
            return true;
        }
        for (String str : strs) {
            if (isEmpty(str)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isEquals(String str1, String str2) {
        if (isEmpty(str1) || isEmpty(str2)) {
            return false;
        }
        return str1.equals(str2);
    }

    public static boolean isAnyEquals(String str1, String... strn) {
        if (isEmpty(str1) || isEmpty(strn)) {
            return false;
        }

        for (String str : strn) {
            if (isEquals(str1, str)) {
                return true;
            }
        }
        return false;
    }

    public static String analyseContentType(String fileFullPath) {
        try {
            Path path = Paths.get(fileFullPath);
            return Files.probeContentType(path);
        } catch (IOException e) {
            return null;
        }
    }

}
